package com.kchouse.homepage;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.gxz.PagerSlidingTabStrip;
import com.kchouse.ListFragment;
import com.kchouse.R;
import com.kchouse.bean.JobType;
import com.kchouse.bean.ProjectType;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;

import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class RecruitmentActivity extends BaseActivity implements View.OnClickListener{

    private RecruitmentActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private PagerSlidingTabStrip pst;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    //声明pst的标题
    String[] titles = {"洗车工", "修车工", "美容师傅","前台招待", "喷漆师傅"};
    private ArrayList<JobType> listproject = new ArrayList<>();//项目分类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recruitment);

        mContent = this;
        intiData();
        intiView();
    }


    /**
     * 获取招聘类别
     */
    private void GetjobType() {

        String url = API.GetjobType;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    String datastr = json.getString("data");
                    JSONObject dasejson = JSONObject.parseObject(datastr);

                    listproject.addAll(JSONObject.parseArray(dasejson.getString("types"), JobType.class));
//                    listproject2.addAll(JSONObject.parseArray(datastr, ProjectType.class));
//
                    intiPSTS();
//                    intiPSTS2();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }


    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("人才招聘");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

        intiPSTS();
    }


    private void intiData() {

        GetjobType();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_back:
                finish();
                break;
        }
    }


    /**
     * 初始化单品tab
     */
    private void intiPSTS() {

        pst = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.pager);

        fragments.clear();
        for (int i = 0; i < listproject.size(); i++) {
            JobType item = listproject.get(i);

            fragments.add(new JobFragment(item.getId()));
//            ListFragment list1 = new ListFragment(i,"招聘");
//            fragments.add(list1);
        }
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        pst.setViewPager(viewPager);
        viewPager.setCurrentItem(0);

// 设置Tab的分割线的颜色
        pst.setDividerColor(getResources().getColor(R.color.colorred));
// 设置分割线的上下的间距,传入的是dp
        pst.setDividerPaddingTopBottom(10);
////设置Tab底部线的颜色
        pst.setUnderlineColor(getResources().getColor(R.color.bg_line));

// 设置Tab 指示器Indicator的高度,传入的是dp
        pst.setIndicatorHeight(3);
// 设置Tab Indicator的颜色
        pst.setIndicatorColor(getResources().getColor(R.color.colorred));

// 设置Tab标题文字的大小,传入的是sp
        pst.setTextSize(13);
// 设置选中Tab文字的颜色
        pst.setSelectedTextColor(getResources().getColor(R.color.colorred));
//设置正常Tab文字的颜色
        pst.setTextColor(getResources().getColor(R.color.colorblack));
//设置Tab文字的左右间距,传入的是dp
        pst.setTabPaddingLeftRight(12);

//是否支持动画渐变(颜色渐变和文字大小渐变)
        pst.setFadeEnabled(false);
// 设置最大缩放,是正常状态的0.3倍
//        pst.setZoomMax(0.3F);
        pst.setZoomMax(0);

//这是当点击tab时内容区域Viewpager是否是左右滑动,默认是true
        pst.setSmoothScrollWhenClickTab(true);
    }


//    /**
//     * 初始化tab
//     */
//    private void intiPSTS() {
//
//        pst = (PagerSlidingTabStrip) findViewById(R.id.tabs);
//        viewPager = (ViewPager) findViewById(R.id.pager);
//        ListFragment list1 = new ListFragment(2,"招聘");
//        ListFragment list2 = new ListFragment(3,"招聘");
//        ListFragment list3 = new ListFragment(4,"招聘");
//        ListFragment list4 = new ListFragment(5,"招聘");
//        ListFragment list5 = new ListFragment(6,"招聘");
////        //添加fragment到集合中时注意顺序
//        fragments.add(list1);
//        fragments.add(list2);
//        fragments.add(list3);
//        fragments.add(list4);
//        fragments.add(list5);
//        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
//        pst.setViewPager(viewPager);
//        viewPager.setCurrentItem(0);
//
//// 设置Tab的分割线的颜色
//        pst.setDividerColor(getResources().getColor(R.color.colorred));
//// 设置分割线的上下的间距,传入的是dp
//        pst.setDividerPaddingTopBottom(10);
//////设置Tab底部线的颜色
//        pst.setUnderlineColor(getResources().getColor(R.color.bg_line));
//
//// 设置Tab 指示器Indicator的高度,传入的是dp
//        pst.setIndicatorHeight(3);
//// 设置Tab Indicator的颜色
//        pst.setIndicatorColor(getResources().getColor(R.color.colorred));
//
//// 设置Tab标题文字的大小,传入的是sp
//        pst.setTextSize(13);
//// 设置选中Tab文字的颜色
//        pst.setSelectedTextColor(getResources().getColor(R.color.colorred));
////设置正常Tab文字的颜色
//        pst.setTextColor(getResources().getColor(R.color.colorblack));
////设置Tab文字的左右间距,传入的是dp
//        pst.setTabPaddingLeftRight(12);
//
////是否支持动画渐变(颜色渐变和文字大小渐变)
//        pst.setFadeEnabled(false);
//// 设置最大缩放,是正常状态的0.3倍
////        pst.setZoomMax(0.3F);
//        pst.setZoomMax(0);
//
////这是当点击tab时内容区域Viewpager是否是左右滑动,默认是true
//        pst.setSmoothScrollWhenClickTab(true);
//    }

    /**
     * * 自定义适配器
     */
    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }//设置每页的标题

        @Override
        public CharSequence getPageTitle(int position) {
            return listproject.get(position).getName();
        }

        //设置每一页对应的fragment
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        //fragment的数量
        @Override
        public int getCount() {
            return fragments.size();
        }
    }

}
